import {
	walletGetAddress,
	walletDeladdress,
	walletList
} from '@/api/index.js'
import {
	getToken
} from '@/utils/auth'

const state = {
	walletList: [],
	walletSelectList: [],
	ferredList: [],
	balancesList: {},
	daywallet: 0,
	totalwallet: 0,
	changeWallet: [],
	currencyList: [],
}

const mutations = {
	SET_CURRENCYLIST: (state, currencyList) => {
		state.currencyList = currencyList
	},
	SET_WALLETLIST: (state, walletList) => {
		state.walletList = walletList
	},
	SET_WALLETSELECTLIST: (state, walletSelectList) => {
		state.walletSelectList = walletSelectList
	},
	SET_FERREDLIST: (state, ferredList) => {
		state.ferredList = ferredList
	},
	SET_BALANCESLIST: (state, balancesList) => {
		state.balancesList = balancesList
	},
	SET_DAYWALLET: (state, daywallet) => {
		state.daywallet = daywallet
	},
	SET_TOTALWALLET: (state, totalwallet) => {
		state.totalwallet = totalwallet
	},
	SET_CHANGEWALLET: (state, changeWallet) => {
		state.changeWallet = changeWallet
	},
}

const actions = {
	// get wallet list
	getWalletList({
		commit
	}) {
		return new Promise((resolve, reject) => {
			walletGetAddress({
				token: getToken()
			}).then(response => {
				commit('SET_WALLETLIST', response.res || [])
				if (response.res.length) {
					let arry = []
					for (let i in response.res) {
						let obj = {
							title: response.res[i].address,
							id: response.res[i].id
						}
						arry.push(obj)
					}
					commit('SET_WALLETSELECTLIST', arry)
				}
				resolve()
			}).catch(() => {
				reject()
			})
		})
	},
	// delete wallet
	walletDelete({
		commit,
		state
	}, id) {
		return new Promise((resolve, reject) => {
			walletDeladdress({
				token: getToken(),
				address_id: id
			}).then(response => {
				if (response.error) return reject();
				let arr = JSON.parse(JSON.stringify(state.walletList)),
					data = [];
				for (let i in arr) {
					if (arr[i].id != id) {
						data.push(arr[i])
					}
				}
				commit('SET_WALLETLIST', data)
				resolve();
			})
		})
	},
	// get balances list
	getbalancesList({
		commit
	}) {
		return new Promise((resolve, reject) => {
			walletList({
				token: getToken()
			}).then(res => {
				if (res.error) return reject();
				commit('SET_DAYWALLET', res.res.daywallet)
				commit('SET_TOTALWALLET', res.res.totalwallet)
				let obj = {
					change_wallet: res.res.change_wallet,
					legal_wallet: res.res.legal_wallet,
					lever_wallet: res.res.lever_wallet,
				}
				commit('SET_BALANCESLIST', obj)
				commit('SET_CHANGEWALLET', obj.change_wallet.balance)
				let arry = []
				let currencyArry = []
				for (let i in obj) {
					let j = {
						title: obj[i].walletname,
						id: obj[i].wallettyp,
						rate: obj[i].totle,
					}
					for (let o in obj[i].balance) {
						if (obj[i].walletname === '周期') {
							j['balance'] = obj[i].balance[o].legal_balance
						}
						if (obj[i].walletname === '点位') {
							j['balance'] = obj[i].balance[o].lever_balance
						}
						if (obj[i].walletname === '币币' && obj[i].balance[o].currency_name === 'USDT') {
							j['balance'] = obj[i].balance[o].change_balance
						}
						
						if (obj[i].walletname === '币币') {
							let currencyObj = {
								title: obj[i].balance[o].currency_name,
								id: obj[i].balance[o].currency,
								rate:obj[i].balance[o].pb_price,
								value: obj[i].balance[o].change_balance,
								link: obj[i].balance[o].currency_link
							}
							currencyArry.push(currencyObj)
						}
					}
					arry.push(j)
				}
				commit('SET_CURRENCYLIST', currencyArry || [])
				commit('SET_FERREDLIST', arry)
			})
		})


	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
